package com.neusoft.mid.msf.common.util;

import com.sun.org.apache.regexp.internal.RE;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @program: pangu
 * @description: 查询参数对象，存储分页查询的相关参数
 * @author: fengjd
 * @create: 2022-07-21 19:22
 **/
@Data
public class Query extends LinkedHashMap<String, Object> {

    private  static final long serialVersionUID = 1L;
    private int page = 1;//当前页码
    private int limit = 10;//每页条数
    private List<OrderItem> orders; //翻页格式 id:asc

    /**
     * Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
     * the same mappings as the specified map.  The <tt>LinkedHashMap</tt>
     * instance is created with a default load factor (0.75) and an initial
     * capacity sufficient to hold the mappings in the specified map.
     *
     * @param m the map whose mappings are to be placed in this map
     * @throws NullPointerException if the specified map is null
     */
    public Query(Map<String, Object> params) {
        this.putAll(params);
        if (params.get("page") != null) {
            this.page = Integer.parseInt(params.get("page").toString());
        }

        if (params.get("limit") != null) {
            this.page = Integer.parseInt(params.get("limit").toString());
        }

        if (params.get("orders") != null) {
            String ordersStr = (String) params.get("orders");
            if (StringUtils.isNotEmpty(ordersStr)) {
                ArrayList<OrderItem> orderItems = new ArrayList<>();
                String[] items = StringUtils.split(ordersStr, ",");

                if ((items != null && items.length > 0)) {
                    for (String item : items) {
                        String[] tempArr = StringUtils.split(item, ",");
                        OrderItem orderItem = new OrderItem();
                        orderItem.setProperty(tempArr[0]);
                        orderItem.setDirection(tempArr[1]);
                        orderItems.add(orderItem);
                    }
                }

                this.orders = orderItems;
            }
        }

        this.remove("page");
        this.remove("limit");
        this.remove("orders");

    }

    public <T> T getQueryFrom(Class<T> clz) {
        T queryForm = null;
        try {
            queryForm = clz.newInstance();
            EntityUtils.copyProperties(queryForm,this);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return queryForm;

    }

    /**
     * 是否翻页查询
     * @return
     */
    public boolean isPaging(){
        return this.page > 0;
    }

    public static Query getInstance(Object bean){
        return getInstance(10,-1,bean);
    }


    public static Query getInstance(int limit, int page, Object bean ) {

        Map<String,Object> params = null;
        if (bean != null) {
                params = EntityUtils.describe(bean);
        }else {
            params = new HashMap<>();
        }

        params.put("page", page);
        params.put("limit", limit);
        return new Query(params);
    }




    @Data
    public static class OrderItem {
        private String property;
        private String direction = "asc";

    }
}
